AC_INIT([cve-check-tool], 5.6.4, [michael.i.doherty@intel.com], [cve-check-tool], [https://github.com/ikeydoherty/cve-check-tool])
AM_INIT_AUTOMAKE([-Wno-portability no-dist-gzip dist-xz foreign subdir-objects])
AC_PROG_CC
AC_PROG_CC_STDC
LT_PREREQ(2.2)
AC_CONFIG_HEADERS([config.h])
AC_PREFIX_DEFAULT(/usr/local)
AM_SILENT_RULES([yes])
LT_INIT([disable-static])
AC_CONFIG_MACRO_DIR([m4])
AX_VALGRIND_CHECK

m4_define([glib_required_version], [2.36.0])
m4_define([libxml2_required_version], [2.9.1])
m4_define([curl_required_version], [7.29.0])
m4_define([gobject_required_version], [2.0])
m4_define([check_required_version], [0.9])
m4_define([json_required_version], [0.16.0])
m4_define([openssl_required_version],[1.0.0])
# TODO: Set minimum sqlite

AC_CHECK_FUNCS_ONCE(malloc_trim)

PKG_CHECK_MODULES(CVE_CHECK_TOOL,
                 [
                  glib-2.0 >= glib_required_version,
                  gio-2.0 >= glib_required_version,
                  libxml-2.0 >= libxml2_required_version,
                  libcurl >= curl_required_version,
                  gobject-2.0 >= gobject_required_version,
                  sqlite3,
                  openssl >= openssl_required_version
                  ])

PKG_CHECK_MODULES(MODULE_COMMON,
                 [
                  libxml-2.0 >= libxml2_required_version,
                  glib-2.0 >= glib_required_version
                 ])

PKG_CHECK_MODULES(CHECK,
    check >= check_required_version,
    [have_check=yes], [have_check=no]
)

AM_CONDITIONAL(ENABLE_UNIT_TESTS, test x$have_check = xyes)

have_coverage=no
AC_ARG_ENABLE(coverage, AS_HELP_STRING([--enable-coverage], [enable test coverage]))
if test "x$enable_coverage" = "xyes" ; then
        AC_CHECK_PROG(lcov_found, [lcov], [yes], [no])
        if test "x$lcov_found" = xno ; then
                AC_MSG_ERROR([*** lcov support requested but the program was not found])
        else
                lcov_version_major="`lcov --version | cut -d ' ' -f 4 | cut -d '.' -f 1`"
                lcov_version_minor="`lcov --version | cut -d ' ' -f 4 | cut -d '.' -f 2`"
                if test "$lcov_version_major" -eq 1 -a "$lcov_version_minor" -lt 10; then
                        AC_MSG_ERROR([*** lcov version is too old. 1.10 required])
                else
			have_coverage=yes
			AC_DEFINE([COVERAGE], [1], [Coverage enabled])
                fi
        fi
fi
AM_CONDITIONAL([COVERAGE], [test "$have_coverage" = "yes"])

AC_ARG_ENABLE(run_in_tree, AS_HELP_STRING([--enable-run-in-tree], [enable running from git tree]))
if test "x$enable_run_in_tree" = "xyes" ; then
	AC_DEFINE([INTREE], [1], [In-tree usage enabled])
else
        enable_run_in_tree="no"
fi

AM_CONDITIONAL([INTREE], [test "$enable_run_in_tree" = "yes"])


AC_ARG_ENABLE(relative_plugins, AS_HELP_STRING([--enable-relative-plugins], [enable relative plugin location lookup]))
if test "x$enable_relative_plugins" = "xyes" ; then
	AC_DEFINE([RELATIVE_PLUGINS], [1], [Relative plugin location lookup])
else
        enable_relative_plugins="no"
fi

AM_CONDITIONAL([RELATIVE_PLUGINS], [test "$enable_relative_plugins" = "yes"])

AC_CONFIG_FILES([Makefile])

AC_OUTPUT

AC_MSG_RESULT([
        cve-check-tool $VERSION
        ========

        prefix:                 ${prefix}
        libdir:                 ${libdir}
        sysconfdir:             ${sysconfdir}
        exec_prefix:            ${exec_prefix}
        bindir:                 ${bindir}
        datarootdir:            ${datarootdir}

        coverage:               ${have_coverage}
        unit-tests:             ${have_check}
        run-in-tree:            ${enable_run_in_tree}

        compiler:               ${CC}
        cflags:                 ${CFLAGS}
        ldflags:                ${LDFLAGS}

        Relative plugins:       ${enable_relative_plugins}
])
